package co.yixiang.modules.system.rest;

import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.RefundDeposit;
import co.yixiang.modules.system.service.RefundDepositService;
import co.yixiang.modules.system.dto.queryCriteria.RefundDepositQueryCriteria;
import co.yixiang.modules.system.dto.RefundDepositDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zf
 * @date 2021-09-11
 */
@AllArgsConstructor
@Api(tags = "退款管理")
@RestController
@RequestMapping("/api/refundDeposit")
public class RefundDepositController {

    private final RefundDepositService refundDepositService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','refundDeposit:list')")
    public void download(HttpServletResponse response, RefundDepositQueryCriteria criteria) throws IOException {
        refundDepositService.download(Convert.toList(RefundDepositDto.class, refundDepositService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询退款")
    @ApiOperation("查询退款")
    @PreAuthorize("@el.check('admin','refundDeposit:list')")
    public ResponseEntity<Object> getRefundDeposits(RefundDepositQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(refundDepositService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增退款")
    @ApiOperation("新增退款")
    @PreAuthorize("@el.check('admin','refundDeposit:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody RefundDeposit resources) {
        return new ResponseEntity<>(refundDepositService.save(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改退款")
    @ApiOperation("修改退款")
    @PreAuthorize("@el.check('admin','refundDeposit:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody RefundDeposit resources, HttpServletRequest req) {
        ResponseEntity<Object> audit = refundDepositService.audit(resources, req);
        return audit;
    }

    @Log("删除退款")
    @ApiOperation("删除退款")
    @PreAuthorize("@el.check('admin','refundDeposit:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        refundDepositService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}